package com.wgj.android.dialog;

import android.app.Dialog;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.entity.Gift;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/15.
 */

public class GiftDialog extends AppCompatDialogFragment{
    @BindView(R.id.grid_view)
    GridView gridView;
    @BindView(R.id.et_gift_num)
    EditText etGiftNum;
    private Dialog dialog;
    private View view;
    private List<Gift> list = new ArrayList<Gift>();
    private GiftAdapter adapter;
    private String money;
    private String giftName;
    private int index;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (view == null) {
            super.onCreate(savedInstanceState);
            view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_gift, null, false);
        }
        ButterKnife.bind(this, view);
        initData();
        initView();
    }

    @Nullable
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (dialog == null) {
            // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
            initDialogStyle(view);
        }
        return dialog;
    }

    private void initDialogStyle(View view) {
        dialog = new Dialog(getActivity(), R.style.dialog_setting);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.popupwindow_anim_style);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        //lp.width = XDensityUtils.getScreenWidth() - XDensityUtils.dp2px(20);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }

    private void initView() {
        adapter = new GiftAdapter();

        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < list.size(); i++) {
                    if (i == position) {
                        index=i;
                        list.get(i).setSelect(true);
                        ViewHolder viewHolder = (ViewHolder) view.getTag();
                        money = viewHolder.textView.getText().toString();
                        giftName = viewHolder.tv_name.getText().toString();
                    } else {
                        list.get(i).setSelect(false);
                    }
                }

                adapter.notifyDataSetChanged();
            }
        });
    }

    private void initData() {
        Gift gift1 = new Gift();
        gift1.setBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.bjl));
        gift1.setGold("100");
        gift1.setName("比基尼");
        list.add(gift1);
        Gift gift2 = new Gift();
        gift2.setBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.beer));
        gift2.setGold("200");
        gift2.setName("啤酒");
        list.add(gift2);
        Gift gift3 = new Gift();
        gift3.setBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.cake));
        gift3.setGold("300");
        gift3.setName("蛋糕");
        list.add(gift3);
        Gift gift4 = new Gift();
        gift4.setBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.flower));
        gift4.setGold("400");
        gift4.setName("鲜花");
        list.add(gift4);
        Gift gift5 = new Gift();
        gift5.setBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.kiss));
        gift5.setGold("500");
        gift5.setName("吻");
        list.add(gift5);
        Gift gift6 = new Gift();
        gift6.setBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.kouhong));
        gift6.setGold("600");
        gift6.setName("口红");
        list.add(gift6);
        Gift gift7 = new Gift();
        gift7.setBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.shoes));
        gift7.setGold("800");
        gift7.setName("高跟鞋");
        list.add(gift7);
        Gift gift8 = new Gift();
        gift8.setBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.watch));
        gift8.setGold("1000");
        gift8.setName("劳力士");
        list.add(gift8);

    }

    @OnClick({R.id.tv_recharge, R.id.tv_give})
    public void recharge(View v) {
        switch (v.getId()) {
            case R.id.tv_recharge:
                XToast.info("暂无提供借口");
                break;
            case R.id.tv_give:
                sendGift();
                listener.onClick(v,etGiftNum.getText().toString(),index);
                break;
        }

    }

    private void sendGift() {
        int num =Integer.parseInt(etGiftNum.getText().toString())*Integer.parseInt(money);
        XHttp.obtain().get(WGJAPI.SEND_GIFT + "?member=" + CacheUtils.getMember().getM_id() + "&money="+num, null, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String str) {
                if(str.equals("余额不足")){
                    XToast.error("余额不足");
                    return;
                }
                XToast.success("赠送"+etGiftNum.getText()+"个" + giftName,2000);
                dismiss();

            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                String sr = "";
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }



    class GiftAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.gift_item, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView.setImageBitmap(list.get(position).getBitmap());
            viewHolder.textView.setText(list.get(position).getGold());
            viewHolder.tv_name.setText(list.get(position).getName());
            if (list.get(position).getSelect()) {
                viewHolder.iv_select.setVisibility(View.VISIBLE);
            } else {
                viewHolder.iv_select.setVisibility(View.GONE);
            }

            return convertView;
        }
    }

    class ViewHolder {
        private ImageView imageView;
        private ImageView iv_select;
        private TextView textView;
        private TextView tv_name;
        private RelativeLayout layout;

        public ViewHolder(View v) {
            imageView = v.findViewById(R.id.image);
            iv_select = v.findViewById(R.id.iv_select);
            textView = v.findViewById(R.id.text);
            tv_name = v.findViewById(R.id.tv_name);
            layout = v.findViewById(R.id.layout);
        }
    }
    private MyDialogListener listener;

    public interface MyDialogListener{
         void onClick(View view,String giftNum,int index);
    }
    public void setMyDialogListener(MyDialogListener dialogListener){
        listener = dialogListener;
    }


}
